package com.angel.activity;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.angel.util.BaseUtil;
import com.angel.util.HttpUtil;
import com.angel.util.StringUtil;
import com.angel.util.UserInfoUtil;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/*获取验证码或者绑定手机号*/
public class Register1Activity extends Activity {
	private Button back_btn;
	private Button next_btn;
	private EditText phoneEdit,password1,password2;
	private TextView titleTxt;
	private Context context;
	private HashMap<String,String> map=new HashMap<String,String>();
	private MyHandler handler;
	private Intent tent;
	private String isBind;//0：表示用于注册功能；1：表示用于绑定手机号；2：表示找回密码
	private ProgressDialog	progressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register1);
		 MobclickAgent.onError(this);//自动捕获异常退出（FC ）
		tent=getIntent();
		isBind=tent.getStringExtra("isBind");
		context=Register1Activity.this;
		progressDialog=new ProgressDialog(this);
		progressDialog.setMessage("请稍后...");
		
		back_btn = (Button)findViewById(R.id.register1_back_btn);
		next_btn = (Button)findViewById(R.id.register1_next_btn);
		phoneEdit=(EditText)findViewById(R.id.reg_phone_edit);
		titleTxt=(TextView)findViewById(R.id.register1_titele_txt);
		
		back_btn.setOnClickListener( new ButtonActionListener());
		next_btn.setOnClickListener( new ButtonActionListener());
		handler=new MyHandler();
		/*动态设置标题*/
		if(isBind.equals("0")){
			titleTxt.setText(getResources().getText(R.string.CreateAccount));
		}else if(isBind.equals("1")){
			titleTxt.setText(getResources().getText(R.string.bindPnoneNum));
		}else if(isBind.equals("2")){
			titleTxt.setText(getResources().getText(R.string.findPassword));
	    }
		
	}

	private class ButtonActionListener implements OnClickListener {
		public void onClick(View v) {
			if (v.getId() == R.id.register1_back_btn) {
				finish();
			}
			if(v.getId() == R.id.register1_next_btn){
				String phoneNum=phoneEdit.getText().toString();
				if(!StringUtil.checkPhoneNum(phoneNum)){
					BaseUtil.makeToast(Register1Activity.this, "请输入正确的手机号");
				}else{
				    sendPhone(phoneNum);
				}
			}
		}
	}
	/*发送填写的手机号*/
	public void sendPhone(String phoneNum){
		map.put("telNum", phoneEdit.getText().toString());
		map.put("isBind", isBind);
		new MyTread().start();
		progressDialog.show();
	}
	class MyTread extends Thread{
		public void run(){
			String url=HttpUtil.baseUrl+"getSecurityCode";
			try {
				String result=HttpUtil.httpPost(url, map);//发送手机号请求，等待验证码
				Message msg=new Message();
				msg.obj=result;
				handler.sendMessage(msg);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	class MyHandler extends Handler{
		public void handleMessage(Message msg){
			 String result=(String)msg.obj;
			 String isSucess=BaseUtil.getValueFromJSON(result);
		     /*处理网络连接超时*/
			 if(isSucess.equals("2")){
				 BaseUtil.makeToast(Register1Activity.this, "对不起,网络连接超时,请稍后重试...");
				 progressDialog.dismiss();
				 return ;
			 }
			map=new HashMap<String,String>();
			try {
				JSONObject obj=new JSONObject(result);
				String isSuccess=obj.getString("isSuccess");
				String identifyCode=obj.getString("identifyCode");
				if(isBind.equals("0")){//注册
					if(isSuccess.equals("0")){//若用户手机号已经存在，在返回0，不需再输入验证码绑定
						BaseUtil.makeToast(Register1Activity.this, "您的手机号已绑定,请重新输入");
					}
					
				}else if(isBind.equals("1")){//绑定手机号
					if(isSuccess.equals("0")){//若用户手机号已经存在，在返回0，不需再输入验证码绑定
						BaseUtil.makeToast(Register1Activity.this, "对不起,该手机号已被绑定");
					//	finish();
					}
				}else if(isBind.equals("2")){//找回密码
					if(isSuccess.equals("0")){//若用户手机号已经存在，在返回0，不需再输入验证码绑定
						BaseUtil.makeToast(Register1Activity.this, "对不起,您的手机号不存在");
					}
				}
				if(isSuccess.equals("1")){//表示可以继续操作
					BaseUtil.makeToast(Register1Activity.this, "您的验证码是:"+identifyCode);
					Intent intent = new Intent();
					intent.putExtra("isBind",isBind);
					intent.putExtra("telNum", phoneEdit.getText().toString());//传递手机号
					intent.putExtra("identifyCode", identifyCode);//存储验证码
					intent.setClass(Register1Activity.this, Register2Activity.class);
					startActivity(intent);
					finish();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}finally{
				progressDialog.dismiss();
			}
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
